<template>
  <el-container>
<el-header>
      <div class="report_header" ref="headerHeight">
        <div class="report_header_left"></div>
        <div style="width: 300px;display: flex;align-items: center;margin-left: 10px;">
          <svg-icon class="primary" :icon-class="$route.meta.icon"/>
          <span style="margin-left: 5px;" class="primary">{{$route.meta.title}}</span>
        </div>
        <div style="display: flex;flex-direction: row;justify-content: flex-end;width: 100%;">
          <div style="display: flex;flex-direction: row;flex-wrap: wrap;overflow: hidden;height:41px;justify-content: flex-end;">
            <div class="report_header_date">
              <el-date-picker
                style="width: 100%;"
                v-model="query.date"
                value-format="yyyy-MM-dd"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions">
              </el-date-picker>
            </div>

			<div class="report_header_query">
			  <el-input v-model="query.orderSn" placeholder="请输入订单号" clearable></el-input>
			</div>

            <div class="report_header_query">
              <el-input v-model="query.title" placeholder="请输入付款单号"></el-input>
            </div>
            <div class="report_header_query">
              <el-select v-model="memberName" clearable placeholder="请选择客户"
                         filterable @focus="getMemberList('')" :filter-method="queryMember" @change="selectMember">
                <el-option v-for="item in members" style="width: 600px;"  :key="item.id" :label="item.name"
                           :value="item">
                  <div style="display: flex">
                    <span style="width: 60px;">{{item.code}}</span>
                    <span style="float: left;width: 100px;overflow: hidden;text-overflow: ellipsis;">{{ item.name }}</span>
                    <span style="float: right; color: #8492a6; font-size: 8px;width: 80px;margin-left: 10px">{{ item.mobile }}</span>
                    <div style="width: 200px;">
                      <span style="color: #8492a6; font-size: 8px;">{{item.address}}</span>
                    </div>
                  </div>
                </el-option>
                <div style="display: flex;width: 100%;position: absolute;bottom: 0">
                  <div style="width: 50%;background-color: rgb(248,248,248);"></div>
                  <div class="elOption" @click="toSearchMember">搜索客户</div>
                </div>
                <div style="height: 30px"></div>
              </el-select>
            </div>
          </div>
          <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="queryPayment">查询</el-button>
        </div>
      </div>
    </el-header>
    <div class="pd10">
        <el-tabs type="border-card" @tab-click="tabsClick" :style="{height:curHeight+'px'}">
          <el-tab-pane label="全部">
            <div class="details_card" v-loadMore="[payLoads,'payment-list1']" :style="{height:tabsHeight+'px'}">
              <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
            </div>
          </el-tab-pane>
          <el-tab-pane label="等待支付">
            <div class="details_card" v-loadMore="[payLoads,'payment-list2']" :style="{height:tabsHeight+'px'}">
              <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
            </div>
          </el-tab-pane>
          <el-tab-pane label="支付成功">
            <div class="details_card" v-loadMore="[payLoads,'payment-list3']" :style="{height:tabsHeight+'px'}">
              <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
            </div>
          </el-tab-pane>
          <el-tab-pane label="超时关闭">
            <div class="details_card" v-loadMore="[payLoads,'payment-list4']" :style="{height:tabsHeight+'px'}">
              <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
            </div>
          </el-tab-pane>
          <el-tab-pane label="支付失败">
            <div class="details_card" v-loadMore="[payLoads,'payment-list5']" :style="{height:tabsHeight+'px'}">
              <paymentList :payments="payments" @detailsClick="detailsClick"></paymentList>
            </div>
          </el-tab-pane>
        </el-tabs>
    </div>
    <el-dialog
      title="双击选择客户"
      :visible.sync="memberTableVisible"
      width="950px"
      v-el-drag-dialog
      append-to-body
      :before-close="memberTableClose">
      <div style="position: relative;">
        <member-table :key="times" @memberDouble="memberDouble" @memberTableClose="memberTableClose"></member-table>
      </div>
    </el-dialog>
    <el-footer class="el-footerBox">
      <pagination :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                  @pagination="loadPayment"></pagination>
    </el-footer>
  </el-container>
</template>

<script>
  import memberTable from '@/components/MemberTable/index'
  import {EventBus} from "@/api/eventBus";
  import loadMore from '@/utils/more.js'
  import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
  import {list} from '../../api/payment'
  import Pagination from '@/components/Pagination';
  import paymentList from './components/list'
  import {list as memberList} from '@/api/member'
import utils from '../../utils/utils.js'
  export default {
    name: 'payment-list',
    directives: {loadMore,elDragDialog},
    activated() {
      this.payLoads = new Date().getTime();
    },
    data() {
      return {
        payLoads: '',
        times:'',
        memberTableVisible:false,
        queryType: true,
        curHeight: null,
        tabsHeight: null,
        memberName:'',
        total: 0,
        orderStatus:'',
        query: {
          date: [],
          title: '',
          memberId: '',
          orderSn: '',
        },
        pageNum: 1,
        pageSize: 20,
        keyword: {
          date: [],
          title: '',
          memberId: '',
          orderSn: '',
        },
        payments: [],
        members: [],
        pickerOptions: {//时间快捷选择
          shortcuts: [{
            text: '昨天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      }
    },
    components: {
      Pagination, paymentList,memberTable
    },
    created() {
      EventBus.$on('payment-details', res => { // 监听状态更新
        for (var i = 0; i < this.payments.length; i++) {
          if (this.payments[i].id == res.id) {
            this.$set(this.payments, i, res)
          }
        }
      })
      this.loadPayment()
    },
    mounted() {
      this.comeIn();
      this.beforeMount()
    },
    methods: {
      memberDouble(data){
        this.memberName=data.name
        this.query.memberId=data.id
        this.memberTableVisible = false
      },
      memberTableClose(){
        this.memberTableVisible = false
      },
      selectMember(data){
        if(!utils.isNull(data)){
          this.memberName=data.name
          this.query.memberId=data.id
        }else{
           this.query.memberId=''
        }

      },
      toSearchMember() {
        this.times=new Date().getSeconds()
        this.memberTableVisible = true
      },
      resetClick() {
        this.query.date = [];
        this.query.orderSn = ''
        this.memberName=''
        this.query.title = '';
        this.query.memberId = "";
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadPayment()
      },
      queryPayment() {
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadPayment()
      },
      detailsClick(data) {
        this.$router.push({
          path: '/payment/view', query: {id: data}
        })
      },
      loadPayment() {
        if(utils.isNull(this.keyword.date)){
          this.keyword.date = []
        }
        let params = new URLSearchParams();
        params.append('pageNum', this.pageNum);
        params.append('pageSize', this.pageSize);
        params.append('status', this.orderStatus);
        params.append('orderSn', this.keyword.orderSn)
        params.append('sn', this.keyword.title);
        params.append('memberId', this.keyword.memberId)
        params.append('startDate', this.keyword.date.length > 0 ? this.keyword.date[0] : '');
        params.append('endDate', this.keyword.date.length > 0 ? this.keyword.date[1] : '');
        list(params).then(response => {
          this.total = response.data.total
          this.payments = response.data.data
        })
      },
      beforeMount() {
        var _this = this
        window.onresize = () => {
          return (() => {
            if (_this.queryType == false) {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 270//减去页面上固定高度height
              _this.tabsHeight = h - 381
            } else {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 224//减去页面上固定高度height
              this.tabsHeight = h - 294
            }
          })();
        };
      },
      comeIn() {
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 224//减去页面上固定高度height
        this.tabsHeight = h - 294
      },
      chooseClick() {
        this.queryType = !this.queryType
        if (this.queryType == false) {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 270//减去页面上固定高度height
          this.tabsHeight = h - 381
        } else {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 224//减去页面上固定高度height
          this.tabsHeight = h - 294
        }
      },
      queryMember(query) {
        this.getMemberList(query)
      },
      getMemberList(query) {
        let param = new URLSearchParams()
        param.append('keyword', query)
        memberList(param).then(res => {
          this.members = res.data.data
        })
      },
      tabsClick(data) {
        var _this = this
        console.log(data)
        switch (data.paneName) {
          case '0':
            _this.orderStatus = '' // 订单状态
            break;
          case '1':
            _this.orderStatus = 0 // 订单状态
            break;
          case '2':
            _this.orderStatus = 1 // 订单状态
            break;
          case '3':
            _this.orderStatus = 2 // 订单状态
            break;
          case '4':
            _this.orderStatus = 3 // 订单状态
            break;
        }
        this.loadPayment()
      }
    }
  }
</script>

<style scoped>
  .details_card {
    overflow: auto;
    padding-left: 20px;
    padding-right: 20px;
    box-sizing: border-box;
    border: 1px solid #f2f2f2;
    border-radius: 4px;
  }
  .elOption{
    background-color: #909399;
    color:white ;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 50%;
    height: 40px
  }
  .details_card:hover {
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
  }

  .app {
    background-color: rgba(248, 248, 248, 0.8);
  }

  /deep/.el-header {
    padding: 10px;
    /* height: 100% !important; */
    padding-bottom: 0;
  }

  .report_header {
    width: 100%;
    min-height: 40px;
    box-sizing: border-box;
    background-color: rgba(248, 248, 248, 0.8);
    display: flex;
    align-items: center;
    padding-bottom: 5px;
    position: relative;
    padding-left: 5px;
    padding-right: 5px;
  }
  .report_header_left {
    width: 5px;
    height: 100%;
    background-color: #409EFF;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
  }

  .report_header_query {
    margin-left: 10px;
    width: 180px;
    margin-top: 5px;
  }
  .report_header_date {
      margin-left: 10px;
      width: 250px;
      margin-top: 5px;
    }
</style>
